package com.hunan.api.catv.service.impl.tvv3;

import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.dto.TvQueryTerminalInfo;
import com.hunan.api.catv.entity.dto.TvQueryTimeLimitInfo;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/10/12 0012
 * @package com.hunan.api.catv.service.impl.tv
 * @company 湖南有线
 */
@Service
public class TvV3Service {
    private static Logger log = LoggerFactory.getLogger(TvV3Service.class);

    @Autowired
    RestTemplate restTemplate;

    public JsonObject getAccesstoken(String domain, Map<String, Object> body) {
        try {
            StringBuilder url = new StringBuilder(domain);
            url.append("/account/get_accesstoken");
//        String post = HttpUtil.post(url.toString(), body);
            HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(body), getHeader());
            ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("获取token失败:{}", e);
        }
        return null;
    }

    public JsonObject setTerminalInfo(String domain, TvQueryTerminalInfo terminalInfo) {
        try {
            StringBuilder url = new StringBuilder(domain);
            url.append("/account/user/adjust_info");
            HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(terminalInfo), getHeader());
            ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("设置TV终端信息失败:{}", e);
        }
        return null;
    }


    public JsonObject setTimeLimit(String domain, TvQueryTimeLimitInfo timeLimitInfo) {
        try {
            StringBuilder url = new StringBuilder(domain);
            url.append("/account/admin/timelimit");
            HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(timeLimitInfo), getHeader());
            ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("设置TV控制时间失败:{}", e);
        }
        return null;
    }

    public JSONObject queryHistoryList(String domain, Map<String, Object> body) {
        try {
            StringBuilder url = new StringBuilder(domain);
            url.append("/history/get_list?");
            String query = URLUtil.buildQuery(body, Charset.forName("utf-8"));
            url.append(query);
            ResponseEntity<String> response = restTemplate.getForEntity(url.toString(), String.class, body);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                Object o = GsonUtils.fromJson(response.getBody(), Object.class);
                return JSONUtil.parseObj(o);
    //            return  JSONUtil.parseObj(response.getBody());
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("查询TV历史记录失败:{}", e);
        }
        return null;
    }

    public JSONObject queryUserList(String domain, Map<String, Object> body) {
        try {
            StringBuilder url = new StringBuilder(domain);
            url.append("/account/user/get_list?");
            String query = URLUtil.buildQuery(body, Charset.forName("utf-8"));
            url.append(query);
            ResponseEntity<String> response = restTemplate.getForEntity(url.toString(), String.class, body);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return JSONUtil.parseObj(response.getBody());
    //            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("查询TV用户信息失败:{}", e);
        }
        return null;
    }

    public JSONObject queryTimeLimit(String domain, Map<String, Object> body) {
        try {
            StringBuffer url = new StringBuffer(domain);
            url.append("/account/admin/get_restrict?");
            String query = URLUtil.buildQuery(body, Charset.forName("utf-8"));
            url.append(query);
            ResponseEntity<String> response = restTemplate.getForEntity(url.toString(), String.class, body);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return JSONUtil.parseObj(response.getBody());
    //            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("查询控制时间失败:{}", e);
        }
        return null;
    }

    public HttpHeaders getHeader() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
//        headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE);
        return headers;
    }


}
